package fun.flyee.android.pengshan.receivers;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import com.hcvsa.MyHCVSA;

import org.greenrobot.eventbus.EventBus;

import fun.flyee.android.pengshan.events.PhoneCallStateEvent;
import hcvs.hcvsa.HCVSA;
import hcvs.hcvsa.SystemEvent;

public class PhoneReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            // 外拨电话
            sendSysPhoneCallCMD("拨打电话");
        } else if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
            // 接通电话
            TelephonyManager tManager = (TelephonyManager) context
                    .getSystemService(Service.TELEPHONY_SERVICE);
            if (tManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
                sendSysPhoneCallCMD("接通电话");
            }
        }
    }

    private void sendSysPhoneCallCMD(String state) {
        EventBus.getDefault().post(new PhoneCallStateEvent(state));
        if (MyHCVSA.getLoginStatus() == SystemEvent.HCVS_LOGIN_STATUS_LOGINED && MyHCVSA.getInstance().getUserSessionMoudle().IsCalling()) {
            MyHCVSA.getInstance().getUserSessionMoudle().SendCallOtherCommandToUser(0, 0, 1);
        }
    }
}